package com.yksj.healthtalk.ui.news;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.server.ServerCenterContentActivity;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 
 * 客户收藏
 * @author zhao
 */
public class NewsCollectionActivity extends BaseFragmentActivity implements OnClickListener,OnItemClickListener {
	
	ListView mListView;
	CollectionAdapter adapter;
	HashMap<String,String> mCaheMap;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.news_collection_layout);
		initView();
//		initData();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		JSONObject jsonObject = adapter.getItem(position);
		String newsid = adapter.getNewsId(jsonObject);
		if("news".equals(adapter.getType(jsonObject))){
		Intent intent = new Intent(this,NewsContentActivity.class);
		intent.putExtra("newsId",newsid);
		intent.putExtra("title","新闻");
		intent.putExtra("type",adapter.getType(jsonObject));
		intent.putExtra("isCollection",true);
		startActivity(intent);}
		else{
			queryNews(newsid);
		}
	}
	
	void queryNews(final String id){
			HttpRestClient.doHttpQueryNewsContentDetail(SmartFoxClient.getLoginUserId(),id,"center",new JsonHttpResponseHandler(this){
				@Override
				public void onSuccess(int statusCode, JSONObject response) {
					super.onSuccess(statusCode, response);
					/*onBundData();
					mJsonObject = response;
					onParseData(response);
					onDoQueryComment("0","0","5");*/
					Intent intent = new Intent(NewsCollectionActivity.this,ServerCenterContentActivity.class);
					intent.putExtra("newsId", id);
					intent.putExtra("type", response.optString("NEWS_TYPE"));
					intent.putExtra("isTrue", "isTrue");
					intent.putExtra("content",response.toString());
					startActivity(intent);
				}
				@Override
				public void onFailure(Throwable error, String content) {
					super.onFailure(error, content);
				}
			});
	}
	
	//新闻收藏列表
	private void initData(){
		HttpRestClient.doHttpQueryNewsCollection(SmartFoxClient.getLoginUserId(),new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, String content) {
				if(statusCode != 200)return;
				List<JSONObject> list = new ArrayList<JSONObject>();
				try {
					JSONArray array = new JSONArray(content);
					for (int i = 0; i < array.length(); i++) {
						list.add(array.getJSONObject(i));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				adapter = new CollectionAdapter();
				adapter.onChangeData(list);
				mListView.setAdapter(adapter);
			}
		});
	}
	
	private void initView(){
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleLeftBtn.setText("返回");
		titleTextV.setText("新闻收藏");
		mListView = (ListView)findViewById(android.R.id.list);
		mListView.setOnItemClickListener(this);
		mListView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				menu.add(0,0,0,"删除");
			}
		});
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();
		int position = menuInfo.position;
		onUpdateData(position);
		return super.onContextItemSelected(item);
	}
	
	private void onUpdateData(int position){
		final JSONObject jsonObject = adapter.getItem(position);
		String id = adapter.getNewsId(jsonObject);
		String newsType=adapter.getType(jsonObject);
		HttpRestClient.doHttpNewsCollectionRemove(SmartFoxClient.getLoginUserId(),id,newsType,new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, String content) {
				if("1".equals(content)){
					adapter.arrayList.remove(jsonObject);
					adapter.notifyDataSetChanged();
					ToastUtil.showToastPanl("已取消收藏");
				}else{
					onFailure(null, null);
				}
			}
		});
	}
	
	class CollectionAdapter extends BaseAdapter{
		final List<JSONObject> arrayList = new ArrayList<JSONObject>();
		@Override
		public int getCount() {
			return arrayList.size();
		}

		@Override
		public JSONObject getItem(int position) {
			return arrayList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
		
		public String getNewsId(JSONObject jsonObject){
			return jsonObject.optString("MENUCODE","");
		}
		
		public void onChangeData(List<JSONObject> list){
			arrayList.addAll(list);
		}
		
		public String getTitleName(JSONObject jsonObject) throws JSONException{
			return jsonObject.getString("MENUNAME");
		}
		
		public String getTime(JSONObject jsonObject) throws JSONException{
			return jsonObject.getString("MENUTIME");
		}
		
		public String getType(JSONObject jsonObject){
			return jsonObject.optString("TYPE");
		}
		/* (non-Javadoc)
		 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			JSONObject jsonObject = getItem(position);
			if(convertView == null){
				LayoutInflater inflater = LayoutInflater.from(NewsCollectionActivity.this);
				convertView = inflater.inflate(R.layout.news_collection_list_item,null);
			}
			TextView textView = (TextView)convertView.findViewById(R.id.titleTxt);
			TextView textView2 = (TextView)convertView.findViewById(R.id.timeTxt);
			try {
				String title = getTitleName(jsonObject);
				String time = getTime(jsonObject);
				textView.setText(title);
				textView2.setText(time);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return convertView;
		}
	}
}
